<?php
// +----------------------------------------------------------------------
// | 简易CMS
// +----------------------------------------------------------------------
// | Copyright (C) 2010 Hoping Software Studio.
// +----------------------------------------------------------------------
// | Version $Id$
// +----------------------------------------------------------------------

namespace app\home\model;

use CMS\Model;

/**
 * 文档基础模型
 */
class Member extends Model{

    /* 用户模型自动完成 */
    protected $_auto = array(
        array('login', 0, self::MODEL_INSERT),
        array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function', 1),
        array('reg_time', NOW_TIME, self::MODEL_INSERT),
        array('last_login_ip', 0, self::MODEL_INSERT),
        array('last_login_time', 0, self::MODEL_INSERT),
        array('status', 1, self::MODEL_INSERT),
    );

    /**
     * 登录指定用户
     * @param  integer $uid 用户ID
     * @return boolean      ture-登录成功，false-登录失败
     */
    public function login($username = '', $password = '') {
        if (!$username || !$password) {
            $this->error = '请完整填写登录表单！';
            return false;
        }
        $map = array('username' => $username);
        $user = $this->where($map)->find();
        if (!$user) {
            $this->error = '对不起，此帐号不存在！';
            return false;
        }
        if ($user['password'] != user_md5($password)) {
            $this->error = '对不起，你输入的密码不正确！';
            return false;
        }
        if(1 != $user['status']) {
            $this->error = '用户未激活或已禁用！'; //应用级别禁用
            return false;
        }
        /* 更新登录信息 */
        $data = array(
            'uid'             => $user['uid'],
            'login'           => array('exp', '`login`+1'),
            'last_login_time' => NOW_TIME,
            'last_login_ip'   => get_client_ip(1),
        );
        $this->save($data, array('uid' => $user['uid']));

        /* 记录登录SESSION和COOKIES */
        $auth = array(
            'uid'             => $user['uid'],
            'username'        => $user['username'],
            'last_login_time' => $user['last_login_time'],
        );

        session('user_auth', $auth);
        session('user_auth_sign', data_auth_sign($auth));

        //记录行为
        action_log('user_login', 'member', $user['uid'], $user['uid']);

        return true;
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){
        session('user_auth', null);
        session('user_auth_sign', null);
    }

    public function updateInfo($uid, $password, $data = array()) {
        if (!$uid) {
            $this->error = '用户不存在！';
            return false;
        }
        if (!$password) {
            $this->error = '旧密码不正确！';
            return false;
        }
        if (isset($data['password']) && !$data['password']) {
            unset($data['password']);
        }
        if (!$data) {
            $this->error = '资源不完整！';
            return false;
        }
        $map = array('uid' => $uid);
        $info = $this->where($map)->find();
        if (!$info) {
            $this->error = '用户信息不存在！';
            return false;
        }
        if (md5($password) !== $info['password']) {
            $this->error = '旧密码不正确！';
            return false;
        }
        $data['password'] = md5($data['password']);
        $data = array_merge($info, $data);
        $result = $this->save($data, $map);
        if ($data['password']) {        //如果修改了密码则需要重新登录
            $this->logout();
        }
        return $result;
    }

}
